How can I convert text (ASCII) files to HTML files/documents using
UNIX shell scripting. I know there are utilities out there that use
perl, etc., but was wondering if anyone has used shell scripting.
Thanks in advance.
Suhas
The simplest:
{ echo "<pre>"
cat "$@"
echo "</pre>"
} > text.html
To convert a text file with paragraphs separated by blank lines:
{
echo "<html><head>
<title>$title</title></head>
<body>
<p>"
sed 's/^$/<p>/' "$@"
echo "</body></html>
} > text.html
You can do whatever you want with a shell script.
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2002, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
You can't. You are trying to get an egg from omelette.
--
William Park, Open Geometry Consulting, <openge...@yahoo.ca>
Linux solution for data management and processing.
You mean I'm imagining the dozens of scripts I've written to do
just that?
You get pieces of cooked egg. OP wants the whole eggs in its own shell.
BEGIN {
printf("<html>\n");
printf("<title> %s </title>\n", FILENAME);
printf("<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000EE\"
vlink=\"#990000\" alink=\"#FE0000\">");
}
{ printf("%s<BR>\n",$0); }
END {
printf("</body>\n");
printf("</html>\n");
}
----------------------------------------------------------------
"Suhas" <sgt...@hotmail.com> wrote in message
news:5c440ee2.02121...@posting.google.com...
> Suhas wrote:
> >
> > How can I convert text (ASCII) files to HTML files/documents using
> > UNIX shell scripting. I know there are utilities out there that use
> > perl, etc., but was wondering if anyone has used shell scripting.
>
> The simplest:
>
> { echo "<pre>"
> cat "$@"
> echo "</pre>"
> } > text.html
Even the simplest text to HTML converter needs to "escape" the
characters "&", "<", and ">" to prevent them from being
interpreted. :-)
My attempt...
#!/bin/sh
echo "<pre>"
sed 's/&/\&/g ; s/</\</g ; s/>/\>/g' "$@"
echo "</pre>"
Peter
--
"If I haven't seen further it is because giants have
been standing in my way." -- Peter J. Acklam
Peter> My attempt...
Peter> #!/bin/sh
Peter> echo "<pre>"
Peter> sed 's/&/\&/g ; s/</\</g ; s/>/\>/g' "$@"
Peter> echo "</pre>"
But that's not using "the shell". That's using "sed".
If you're gonna use sed, you might as well use Perl.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
That's like answering: "It's two far to walk, I'll take the car."
with "If you're not going to walk, you might as well drive an
18-wheeler."
>>>>>> "Peter" == Peter J Acklam <pjac...@online.no> writes:
>
> Peter> My attempt...
>
> Peter> #!/bin/sh
>
> Peter> echo "<pre>"
> Peter> sed 's/&/\&/g ; s/</\</g ; s/>/\>/g' "$@"
> Peter> echo "</pre>"
>
> But that's not using "the shell". That's using "sed".
> If you're gonna use sed, you might as well use Perl.
Sure, but this being comp.unix.shell, I post shell scripts unless
there is a real benefit from using perl.
Besides, there are more computers with "sed" installed than with
"perl" installed.
Peter
--
I wish dialog boxes had a butten saying "Whatever". I hate being
forced to answer "Yes" or "No" to a question I have no opinion on
whatsoever. There ought to be a button matching my indifference.
> Randal L. Schwartz wrote:
>
> > But that's not using "the shell". That's using "sed".
> > If you're gonna use sed, you might as well use Perl.
>
> That's like answering: "It's two far to walk, I'll take the
> car." with "If you're not going to walk, you might as well
> drive an 18-wheeler."
I see your point, but considering that perl is both faster and
more flexible than the shell, I'm not sure if "18-wheeler" vs
"car" is a good analogy for "perl" vs "shell". :-)
> But that's not using "the shell". That's using "sed".
> If you're gonna use sed, you might as well use Perl.
Huh - c'mon, surely you know better:
Utilities which are installed by tradition and which are very
useful in shell scripts, are on-topic here. Let alone awk(1)...
perl(1) in contrast has its own group,
so what's your (biased) point? ;-)
Sven
If speed is an issue, I'll use C, which is several orders of
magnitude more efficient than Perl.
> Peter J. Acklam wrote:
>
> > [...] considering that perl is both faster and more flexible
> > than the shell, I'm not sure if "18-wheeler" vs "car" is a
> > good analogy for "perl" vs "shell". :-)
>
> If speed is an issue, I'll use C, which is several orders of
> magnitude more efficient than Perl.
The speed factor depends entirely on what you are doing.
Chris> If speed is an issue, I'll use C, which is several orders of
Chris> magnitude more efficient than Perl.
Maybe for benchmarks you select. Overall, "several orders of
magnitude" is clearly a lie. We've got "typical mix" benchmarks that
show that Perl is even faster than Java for a general e-commerce
system.
Besides, these days, it's I/O that matters. Not CPU, unless you're
simulating weather or a nuclear explosion.
Sven> Randal L. Schwartz wrote:
>> But that's not using "the shell". That's using "sed".
>> If you're gonna use sed, you might as well use Perl.
Sven> Huh - c'mon, surely you know better:
Sven> Utilities which are installed by tradition and which are very
Sven> useful in shell scripts, are on-topic here. Let alone awk(1)...
I bet 80% or more of the "unix" installations have /usr/bin/perl
or /usr/local/bin/perl.
Counting Linux, of course. Which is the "new" tradition.
I wasn't referring to speed, but to usage of resources, primarily
memory.
> Besides, these days, it's I/O that matters. Not CPU, unless you're
> simulating weather or a nuclear explosion.
--
Chris, you're being sucked into a useless flame war. Those who make
income from Perl will never admit their flaky position. Just like I'll
never admit Python can do any wrong. :-)
100% of "Unix" have Awk.
> Chris F.A. Johnson <c.f.a....@rogers.com> wrote:
>
> > I wasn't referring to speed, but to usage of resources,
> > primarily memory.
>
> Chris, you're being sucked into a useless flame war.
Considering what Chris has written in this thread, I consider him
one of the war mongers. :-)
> Those who make income from Perl will never admit their flaky
> position. Just like I'll never admit Python can do any wrong.
> :-)
I think this is being narrow-minded. I use Perl a lot, but I have
no problems admitting that Perl has it's weaknesses and
limitations -- just like *any* other tool or programming language.
In the case of converting text to HTML, which this thread was
about, sed was enough. In other cases, I'll use awk or Perl.
Guilty as charged. :(
Though that was not my intention.